﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_ProductRepertoryHistory")]
    public class T_ProductRepertoryHistory : IEntity<T_ProductRepertoryHistory>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int ProductId { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }
        public int CollegeId { get; set; }
        public int DormitoryId { get; set; }
        public int PutNum { get; set; }
        public DateTime PutDate { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        public override T_ProductRepertoryHistory Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_ProductRepertoryHistory Find(System.Linq.Expressions.Expression<Func<T_ProductRepertoryHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProductRepertoryHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.CollegeId = item.CollegeId;
                    this.DormitoryId = item.DormitoryId;
                    this.Id = item.Id;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.PutDate = item.PutDate;
                    this.PutNum = item.PutNum;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_ProductRepertoryHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProductRepertoryHistory.Any(where);
            }
        }

        public override T_ProductRepertoryHistory FindLast(System.Linq.Expressions.Expression<Func<T_ProductRepertoryHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProductRepertoryHistory.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.CollegeId = item.CollegeId;
                    this.DormitoryId = item.DormitoryId;
                    this.Id = item.Id;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.PutDate = item.PutDate;
                    this.PutNum = item.PutNum;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }
    }
}
